package cn.yunhe.daoImpl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import cn.yunhe.dao.BookDao;
import cn.yunhe.entity.Book;
import cn.yunhe.factory.SqlSessionFactoryUtil;

public class BookDaoImpl implements BookDao {

	public boolean addBook(Book book) {
		SqlSession sqlSession = null;
		int result = 0;
		try {
			sqlSession = SqlSessionFactoryUtil.getSqlSession();
			result = sqlSession.insert("bookMapper.addBook",book);
			sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
			sqlSession.rollback();
		} finally {
			if (sqlSession != null) {
				sqlSession.close();
			}
		}
		return result == 0 ? false : true;
	}

	public boolean deleteBook(Book book) {
		SqlSession sqlSession = null;
		int result = 0;
		try {
			sqlSession = SqlSessionFactoryUtil.getSqlSession();
			result = sqlSession.delete("bookMapper.deleteBook",book);
			sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
			sqlSession.rollback();
		} finally {
			if (sqlSession != null) {
				sqlSession.close();
			}
		}
		return result == 0 ? false : true;
	}

	public List<Book> selectBook(Book book) {
		SqlSession sqlSession = null;
		List<Book> list = null;
		try {
			sqlSession = SqlSessionFactoryUtil.getSqlSession();
			list = sqlSession.selectList("bookMapper.selectBook",book);
		} catch (Exception e) {
			System.out.println("BookDaoImpl---selectBook");
			e.printStackTrace();
		} finally {
			if (sqlSession != null) {
				sqlSession.close();
			}
		}
		return list;
	}

	public boolean updateBook(Book book) {
		SqlSession sqlSession = null;
		int result = 0;
		try {
			sqlSession = SqlSessionFactoryUtil.getSqlSession();
			result = sqlSession.delete("bookMapper.updateBook",book);
			sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
			sqlSession.rollback();
		} finally {
			if (sqlSession != null) {
				sqlSession.close();
			}
		}
		return result == 0 ? false : true;
	}

}
